<?php
function golfscore_save($params){
	$golf_score_id = get_input('golf_score_id');
	$score_type = getIntegerValue(get_input('score_type'));
	$holes_played = getIntegerValue(get_input('holes_played'));
	$month = get_input('month');
	$day = get_input('day');
	$year = get_input('year');
	$course_id = get_input('course');
	$tee = get_input('tee');
	$rating = get_input('rating');
	$slope = get_input('slope');
	
	$score_map = array();
	if($score_type == 1) {
		$score_18 = getIntegerValue(get_input('score_18'));
		$score_total = 0;
		if(isset($score_18) && $score_18 > 0) {
			$score_total += $score_18;
			$score_map['18'] = $score_18;
		}
		$score_17 = getIntegerValue(get_input('score_17'));
		if(isset($score_17) && $score_17 > 0) {
			$score_total += $score_17;
			$score_map['17'] = $score_17;
		}
		$score_16 = getIntegerValue(get_input('score_16'));
		if(isset($score_16) && $score_16 > 0) {
			$score_total += $score_16;
			$score_map['16'] = $score_16;
		}
		$score_15 = getIntegerValue(get_input('score_15'));
		if(isset($score_15) && $score_15 > 0) {
			$score_total += $score_15;
			$score_map['15'] = $score_15;
		}
		$score_14 = getIntegerValue(get_input('score_14'));
		if(isset($score_14) && $score_14 > 0) {
			$score_total += $score_14;
			$score_map['14'] = $score_14;
		}
		$score_13 = getIntegerValue(get_input('score_13'));
		if(isset($score_13) && $score_13 > 0) {
			$score_total += $score_13;
			$score_map['13'] = $score_13;
		}
		$score_12 = getIntegerValue(get_input('score_12'));
		if(isset($score_12) && $score_12 > 0) {
			$score_total += $score_12;
			$score_map['12'] = $score_12;
		}
		$score_11 = getIntegerValue(get_input('score_11'));
		if(isset($score_11) && $score_11 > 0) {
			$score_total += $score_11;
			$score_map['11'] = $score_11;
		}
		$score_10 = getIntegerValue(get_input('score_10'));
		if(isset($score_10) && $score_10 > 0) {
			$score_total += $score_10;
			$score_map['10'] = $score_10;
		}
		$score_9 = getIntegerValue(get_input('score_9'));
		if(isset($score_9) && $score_9 > 0) {
			$score_total += $score_9;
			$score_map['9'] = $score_9;
		}
		$score_8 = getIntegerValue(get_input('score_8'));
		if(isset($score_8) && $score_8 > 0) {
			$score_total += $score_8;
			$score_map['8'] = $score_8;
		}
		$score_7 = getIntegerValue(get_input('score_7'));
		if(isset($score_7) && $score_7 > 0) {
			$score_total += $score_7;
			$score_map['7'] = $score_7;
		}
		$score_6 = getIntegerValue(get_input('score_6'));
		if(isset($score_6) && $score_6 > 0) {
			$score_total += $score_6;
			$score_map['6'] = $score_6;
		}
		$score_5 = getIntegerValue(get_input('score_5'));
		if(isset($score_5) && $score_5 > 0) {
			$score_total += $score_5;
			$score_map['5'] = $score_5;
		}
		$score_4 = getIntegerValue(get_input('score_4'));
		if(isset($score_4) && $score_4 > 0) {
			$score_total += $score_4;
			$score_map['4'] = $score_4;
		}
		$score_3 = getIntegerValue(get_input('score_3'));
		if(isset($score_3) && $score_3 > 0) {
			$score_total += $score_3;
			$score_map['3'] = $score_3;
		}
		$score_2 = getIntegerValue(get_input('score_2'));
		if(isset($score_2) && $score_2 > 0) {
			$score_total += $score_2;
			$score_map['2'] = $score_2;
		}
		$score_1 = getIntegerValue(get_input('score_1'));
		if(isset($score_1) && $score_1 > 0) {
			$score_total += $score_1;
			$score_map['1'] = $score_1;
		}
		$holes_played = count($score_map);
	} else {
		$score_total = getIntegerValue(get_input('score_total'));
	}
	
	$user_guid = elgg_get_logged_in_user_guid();
	
	if (!$course_id) {
		register_error(elgg_echo('golfscore:save:failed'));
		forward(REFERER);
	}
	
	$golf_course = elgg_get_golf_course(array('golf_course_id' => $course_id));
	$golf_course = $golf_course[0];
	
	$golf_course_cards = elgg_get_golf_course_card(array('golf_course_id' => $course_id));
	
	$golf_handicaps = elgg_get_golf_handicap(array('user_entity_id' => $user_guid));
	$golf_handicap == null;
	if(isset($golf_handicaps) && count($golf_handicaps) >= 1) {
		$golf_handicap = $golf_handicaps[0];
		$handicap_index = $golf_handicap->handicap_index;
	} else {
		$handicap_index = -1;
	}
	$current_date = date("Y-m-d H:i:s");
	if (!$golf_score_id || $golf_score_id == 0) {
		$score = new ElggGolfScoreItem();
		$new = true;
	} else {
		$new = false;
		$score = elgg_get_golf_score(array('golf_score_id' => $golf_score_id));
		$score = $score[0];
	}
	$score->golf_course_id = $course_id;
	$score->user_entity_id = $user_guid;
	$score->tee = $tee;
	$score->total_score = $score_total;
	if($holes_played == 18) {
		$escadjustment_score = elgg_golfscore_get_escadjustment_score($handicap_index,$tee,$golf_course,$golf_course_cards,$score_total,$score_map);
	} else {
		$escadjustment_score = $score_total;
	}
	$score->esc_adjustment_total_score = $escadjustment_score;
	$score->course_rating = $rating;
	$score->course_slope = $slope;
	$score->holes_played = $holes_played;
	$score->score_type = $score_type;
	$score->input_date = date('Y-m-d', strtotime($year . '-' . $month . '-' . $day));
	$score->update_date = $current_date;
	$score->active_flg = 1;
	$ret = save_golf_score($score);
	if ($ret) {
	
		elgg_clear_sticky_form('golfscore');
		if($new) {
			$golf_score_id = $ret;
			$score->golf_score_id = $golf_score_id;
		}
	
		if($score_type == 1) {
	
			if(!$new) {
				delete_golf_score_detail($golf_score_id);
			}
	
			$escadjustment_score_map = elgg_golfscore_get_escadjustment_score_map($handicap_index,$tee,$golf_course,$golf_course_cards,$score_total,$score_map);
	
			for($i = 1; $i <= 18; $i++) {
				if($score_map[$i]) {
					$detail = new ElggGolfScoreDetailItem();
					$detail->golf_score_id = $golf_score_id;
					$detail->hole = $i;
					$detail->score = $score_map[$i];
					$detail->esc_adjustment_score = $escadjustment_score_map[$i];
					save_golf_score_detail($detail);
				}
			}
		}
	
		if($holes_played == 18) {
			elgg_golfscore_recalculate_handicap($golf_handicap, $user_guid, $current_date);
		}
	
		$user = elgg_get_logged_in_user_entity();
		golfscore_post($user,$score,$golf_course);
	
		system_message(elgg_echo('golfscore:save:success'));
	} else {
		register_error(elgg_echo('golfscore:save:failed'));
	}
}


?>